package com.dao.impl;

import com.dao.TypeDao;
import com.entity.Type;
import com.util.DruidUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.MapListHandler;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

public class TypeDaoImpl implements TypeDao {

    /**
     * 根据id查询食物类型
     */
    QueryRunner runner = new QueryRunner();
    @Override
    public Type getById(long id) {
        Connection connection = DruidUtils.getConnection();
        String sql = "select * from type where id =?";
        try {
            Type query = runner.query(connection, sql, new BeanHandler<>(Type.class), id);
            return query;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            try {
                connection.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        return null;
    }

    @Override
    public List<Type> getAll() {
        Connection connection = DruidUtils.getConnection();
        String sql = "select * from type";
        try {
            List<Type> query = runner.query(connection, sql, new BeanListHandler<>(Type.class));
            return query;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            try {
                connection.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
            return null;
    }

    @Override
    public int modify(int id,String name) {
        Connection connection = DruidUtils.getConnection();
            String sql="UPDATE type set name=? WHERE id=?";
        try {
            int update = runner.update(connection, sql, name, id);
            return update;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            try {
                connection.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        return 0;
    }

    @Override
    public int add(String name) {
        Connection connection = DruidUtils.getConnection();
        String sql="INSERT into type(name) VALUES(?)";
        try {
            int update = runner.update(connection, sql, name);
            return update;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            try {
                connection.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        return 0;
    }

    @Override
    public int delete(int id) {
        Connection connection = DruidUtils.getConnection();
        String sql="DELETE FROM type WHERE id=?";
        try {
            int update = runner.update(connection, sql, id);
            return update;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            try {
                connection.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        return 0;
    }

    /**
     * 查询类型下面是否有子数据
     * @param id
     * @return
     */
    @Override
    public int getTypeId(int id) {
        Connection connection = DruidUtils.getConnection();
        String sql="SELECT * from foods,type WHERE type_id=type.id and type.id=?";
        try {
            List<Type> query = runner.query(connection, sql, new BeanListHandler<>(Type.class), id);
            return query.size();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            try {
                connection.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        return 0;
    }

    @Override
    public List<Type> getByName(String typeName) {
        Connection connection = DruidUtils.getConnection();
       List<Type> types=null;
        typeName="%"+typeName+"%";
        String sql = "select * from bigdata.type WHERE name LIKE ?;";
        try {
            types = runner.query(connection,sql,new BeanListHandler<>(Type.class),typeName);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {

            try {
                connection.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }

        }
        return types;
    }

    public static void main(String[] args) {
        TypeDao typeDao = new TypeDaoImpl();
        System.out.println(typeDao.getByName("快餐"));
    }
}
